package com.mall.user.domain.model;

import com.mall.goods.common.enums.GoodsPromotionType;
import com.mall.goods.common.helper.PriceHelper;
import com.mall.goods.common.helper.ShopHelper;
import com.mall.goods.common.util.Common;
import com.mall.goods.common.util.State;
import com.mall.goods.common.config.ShopConfig;
import io.micrometer.common.util.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**

 *
 * 商品SPU实体
 * 
 *
 * Created 2017/4/13 11:22
 */
@Data
public class GoodsCommon implements Serializable,Cloneable {
    /**
     * 商品SPU编号
     */
    private int commonId;
    /**
     * 商品名称
     */
    private GoodsName goodsName;
    /**
     * 商品卖点
     */
    private Jingle jingle;
    /**
     * 商品分类编号
     */
    //@Min(value = 1)
    private int categoryId;
    /**
     * 一级分类
     */
    private int categoryId1;
    /**
     * 二级分类
     */
    private int categoryId2;
    /**
     * 三级分类
     */
    private int categoryId3;
    /**
     * 规格JSON
     */
    private String specJson;
    /**
     * 商品编号和规格值编号JSON
     */
    private String goodsSpecValueJson;
    /**
     * 店铺编号
     */
    private int storeId;
    /**
     * 品牌编号
     */
    private BrandId brandId;
    /**
     * 商品状态<br>
     * 0下架，1正常，10违规禁售
     */
    private int goodsState;
    /**
     * 违规禁售原因
     */
    private StateRemark stateRemark;
    /**
     * 审核状态<br>
     * 0未通过，1已通过，10审核中
     */
    private int goodsVerify;
    /**
     * 审核失败原因
     */
    private String verifyRemark;
    /**
     * 商品创建时间
     */
    private Timestamp createTime;
    /**
     * 商品更新时间
     */
    private Timestamp updateTime;
    /**
     * 一级地区编号
     */
    private AreaId1 areaId1 = AreaId1.of(0L);
    /**
     * 二级地区编号
     */
    private AreaId2 areaId2 = AreaId2.of(0L);
    /**
     * 省市县(区)内容
     */
    private String areaInfo;
    /**
     * 商品运费
     */
    private BigDecimal goodsFreight =  new BigDecimal("0");
    /**
     * 运费模板ID
     */
    private int freightTemplateId = 0;
    /**
     * 商品重量
     */
    private BigDecimal freightWeight = BigDecimal.ZERO;
    /**
     * 商品体积
     */
    private BigDecimal freightVolume = BigDecimal.ZERO;
    /**
     * 是否推荐
     */
    private int isCommend;
    /**
     * 起购量0
     */
    private int batchNum0 = 1;
    /**
     * 起购量0  终点
     */
    private int batchNum0End = 0;
    /**
     * 起购量1
     */
    private int batchNum1;
    /**
     * 起购点1 终点
     */
    private int batchNum1End = 0;
    /**
     * 起购量2
     */
    private int batchNum2;
    /**
     * 起购价0
     */
    private BigDecimal batchPrice0;
    /**
     * 起购价1
     */
    private BigDecimal batchPrice1;
    /**
     * 起购价2
     */
    private BigDecimal batchPrice2;
    /**
     * PC端起购价0
     */
    private BigDecimal webPrice0 = new BigDecimal(0);
    /**
     * PC端起购价1
     */
    private BigDecimal webPrice1 = new BigDecimal(0);
    /**
     * PC端起购价2
     */
    private BigDecimal webPrice2 = new BigDecimal(0);
    /**
     * 商品最低价
     */
    private BigDecimal webPriceMin;
    /**
     * PC端促销进行状态
     */
    private int webUsable = 0;
    /**
     * APP端起购价0
     */
    private BigDecimal appPrice0 = new BigDecimal(0);
    /**
     * APP端起购价1
     */
    private BigDecimal appPrice1 = new BigDecimal(0);
    /**
     * APP端起购价2
     */
    private BigDecimal appPrice2 = new BigDecimal(0);
    /**
     * 商品最低价
     */
    private BigDecimal appPriceMin;
    /**
     * APP端促销进行状态
     */
    private int appUsable = 0;
    /**
     * 微信端起购价0
     */
    private BigDecimal wechatPrice0 = new BigDecimal(0);
    /**
     * 微信端起购价1
     */
    private BigDecimal wechatPrice1 = new BigDecimal(0);
    /**
     * 微信端起购价2
     */
    private BigDecimal wechatPrice2 = new BigDecimal(0);
    /**
     * 商品最低价
     */
    private BigDecimal wechatPriceMin;
    /**
     * 微信端促销进行状态
     */
    private int wechatUsable = 0;
    /**
     * 促销编号
     */
    private int promotionId = 0;
    /**
     * 促销开始时间
     */
    private Timestamp promotionStartTime;
    /**
     * 促销结束时间
     */
    private Timestamp promotionEndTime;
    /**
     * 活动状态
     */
    private int promotionState = 0;
    /**
     * 折扣
     */
    private Double promotionDiscountRate = 0.0;
    /**
     * 活动类型
     */
    private int promotionType = GoodsPromotionType.GENERAL;
    /**
     * 活动类型文字
     */
    private String promotionTypeText;
    /**
     * 销售模式
     * 零售1  批发2
     */
    private int goodsModal;
    /**
     * 商品规格名称JSON
     */
    private String goodsSpecNames;
    /**
     * 计量单位
     */
    private String unitName;
    /**
     * 被收藏数量
     */
    private int goodsFavorite = 0;
    /**
     * 被点击数量
     */
    private int goodsClick = 0;
    /**
     * 评价数量
     */
    private Integer evaluateNum = 0;
    /**
     * 好评率
     */
    private GoodsRate goodsRate = GoodsRate.of(100L);
    /**
     * 销售数量
     */
    private int goodsSaleNum = 0;
    /**
     * 图片名称
     */
    private String imageName;
    /**
     * 图片路径
     */
    private String imageSrc;
    /**
     * 是否参加促销
     * 1参加促销
     */
    private int joinBigSale = State.YES;
    /**
     * 商品图片
     */
    private List<GoodsImage> goodsImageList = new ArrayList<>();
    /**
     * 商品列表
     */
    private List<Goods> goodsList = new ArrayList<>();
    /**
     * 是否有赠品
     */
    private Integer isGift;
    /**
     * 拼团编号
     */
    private Integer groupId;
    /**
     * 是否为编辑团购商品
     */
    private int isGroupEdit = 0;
    /**
     * 是否为分销商品
     */
    private Integer isDistribution = 0;
    /**
     * 是否为积分商品
     */
    private Integer isPointsGoods = 0;
    /**
     * 消保编号1
     */
    private Integer contractItem1 = 0;
    /**
     * 消保编号2
     */
    private Integer contractItem2 = 0;
    /**
     * 消保编号3
     */
    private Integer contractItem3 = 0;
    /**
     * 消保编号4
     */
    private Integer contractItem4 = 0;
    /**
     * 消保编号5
     */
    private Integer contractItem5 = 0;
    /**
     * 消保编号6
     */
    private Integer contractItem6 = 0;
    /**
     * 消保编号7
     */
    private Integer contractItem7 = 0;
    /**
     * 消保编号8
     */
    private Integer contractItem8 = 0;
    /**
     * 消保编号9
     */
    private Integer contractItem9 = 0;
    /**
     * 消保编号10
     */
    private Integer contractItem10 = 0;
    /**
     * 搜索优先级
     */
    private Integer searchBoost = 0;
    /**
     * 支持过期退款
     */
    private Integer virtualOverdueRefund = 0;
    /**
     * 海外商品税率
     */
    private BigDecimal foreignTaxRate = BigDecimal.ZERO;


    public BigDecimal getWebPriceMin() {
        BigDecimal price = getWebPrice2();
        if (PriceHelper.isEquals(price, BigDecimal.ZERO)) {
            price = getWebPrice1();
        }
        if (PriceHelper.isEquals(price, BigDecimal.ZERO)) {
            price = getWebPrice0();
        }
        return PriceHelper.format(price);
    }

//    public String getStateRemark() {
//        if(StringUtils.isBlank(stateRemark)){
//            return "";
//        }
//        return stateRemark;
//    }

    public String getVerifyRemark() {
        if(StringUtils.isBlank(verifyRemark)){
            return "";
        }
        return verifyRemark;
    }

    public BigDecimal getFreightWeight() {
        if (freightWeight == null) {
            return BigDecimal.ZERO;
        }
        return freightWeight;
    }

    public BigDecimal getFreightVolume() {
        if (freightVolume == null) {
            return BigDecimal.ZERO;
        }
        return freightVolume;
    }

    public int getBatchNum0End() {
        return (batchNum1 == 0) ? batchNum0End : batchNum1 - 1;
    }

    public int getBatchNum1End() {
        return (batchNum2 == 0) ? batchNum1End : batchNum2 - 1;
    }


    public BigDecimal getWebPrice0() {
        if (getWebUsable() == State.NO || PriceHelper.isEquals(webPrice0, new BigDecimal(0))) {
            return batchPrice0;
        } else {
            return webPrice0;
        }
    }

    public BigDecimal getWebPrice1() {
        if (getWebUsable() == State.NO || PriceHelper.isEquals(webPrice1, new BigDecimal(0))) {
            return batchPrice1;
        } else {
            return webPrice1;
        }
    }


    public BigDecimal getWebPrice2() {
        if (getWebUsable() == State.NO || PriceHelper.isEquals(webPrice2, new BigDecimal(0))) {
            return batchPrice2;
        } else {
            return webPrice2;
        }
    }

    public int getWebUsable() {
        if (webUsable == State.NO || promotionState == State.NO || promotionStartTime.after(ShopHelper.getCurrentTimestamp()) || promotionEndTime.before(ShopHelper.getCurrentTimestamp())) {
            return State.NO;
        } else {
            return State.YES;
        }
    }

    public BigDecimal getAppPrice0() {
        if (getAppUsable() == State.NO || PriceHelper.isEquals(appPrice0, new BigDecimal(0))) {
            return batchPrice0;
        } else {
            return appPrice0;
        }
    }

    public BigDecimal getAppPrice1() {
        if (getAppUsable() == State.NO || PriceHelper.isEquals(appPrice1, new BigDecimal(0))) {
            return batchPrice1;
        } else {
            return appPrice1;
        }
    }

    public BigDecimal getAppPrice2() {
        if (getAppUsable() == State.NO || PriceHelper.isEquals(appPrice2, new BigDecimal(0))) {
            return batchPrice2;
        } else {
            return appPrice2;
        }
    }

    public BigDecimal getAppPriceMin() {
        if (!PriceHelper.isEquals(getAppPrice2(), new BigDecimal(0))) {
            return getAppPrice2();
        } else if (!PriceHelper.isEquals(getAppPrice1(), new BigDecimal(0))) {
            return getAppPrice1();
        } else {
            return getAppPrice0();
        }
    }

    public int getAppUsable() {
        if (appUsable == State.NO || promotionState == State.NO || promotionStartTime.after(ShopHelper.getCurrentTimestamp()) || promotionEndTime.before(ShopHelper.getCurrentTimestamp())) {
            return State.NO;
        } else {
            return State.YES;
        }
    }

    public BigDecimal getWechatPrice0() {
        if (getWechatUsable() == State.NO || PriceHelper.isEquals(wechatPrice0, new BigDecimal(0))) {
            return batchPrice0;
        } else {
            return wechatPrice0;
        }
    }

    public BigDecimal getWechatPrice1() {
        if (getWechatUsable() == State.NO || PriceHelper.isEquals(wechatPrice1, new BigDecimal(0))) {
            return batchPrice1;
        } else {
            return wechatPrice1;
        }
    }

    public BigDecimal getWechatPrice2() {
        if (getWechatUsable() == State.NO || PriceHelper.isEquals(wechatPrice2, new BigDecimal(0))) {
            return batchPrice2;
        } else {
            return wechatPrice2;
        }
    }

    public BigDecimal getWechatPriceMin() {
        if (!PriceHelper.isEquals(getWechatPrice2(), new BigDecimal(0))) {
            return getWechatPrice2();
        } else if (!PriceHelper.isEquals(getWechatPrice1(), new BigDecimal(0))) {
            return getWechatPrice1();
        } else {
            return getWechatPrice0();
        }
    }

    public int getWechatUsable() {
        if (wechatUsable == State.NO || promotionState == State.NO || promotionStartTime.after(ShopHelper.getCurrentTimestamp()) || promotionEndTime.before(ShopHelper.getCurrentTimestamp())) {
            return State.NO;
        } else {
            return State.YES;
        }
    }

    public String getPromotionTypeText() {
        return GoodsPromotionType.getText(promotionType);
    }


    // 修复图片路径获取方法
    public String getImageSrc() {
        // 如果imageSrc为空，尝试重新计算
        if (StringUtils.isBlank(this.imageSrc)) {
            // 使用ShopConfig重新计算图片路径
            if (StringUtils.isBlank(this.imageName)) {
                this.imageSrc = ShopConfig.getPublicRoot() + Common.DEFAULT_GOODS_IMG;
            } else {
                this.imageSrc = ShopConfig.getUploadRoot() + this.imageName;
            }
        }
        return this.imageSrc;
    }

    public Integer getIsGift() {
        if (isGift == null) {
            return 0;
        }
        return isGift;
    }

    public Integer getGroupId() {
        if (groupId == null) {
            return 0;
        }
        return groupId;
    }

    public Integer getIsDistribution() {
        if (isDistribution == null) {
            return 0;
        }
        return isDistribution;

    }

    public Integer getIsPointsGoods() {
        if (isPointsGoods == null) {
            return 0;
        }
        return isPointsGoods;
    }

    @Override
    public GoodsCommon clone() throws CloneNotSupportedException {
        return (GoodsCommon) super.clone();
    }
}
